//
//  AWMainViewController.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/4.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

import UIKit

class AWMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupTabBar()

        addChildViewControllers()
        
    }
    
    private func setupTabBar() {
        
        let tabBar = AWTabBar()
        
        setValue(tabBar, forKey: "tabBar")
        
        tabBar.tabBarDelegate = self
        
        tabBar.callBack = { [unowned self] in
            
            print("我是闭包的回调")
            
            print(self)
            
        }
        
    }

    private func addChildViewController(childController: UIViewController, title: String, imageName: String, index: Int) {
        
        childController.title = title
        
        childController.tabBarItem.image = UIImage(named: imageName)
        
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11)], for: .normal)
        
        childController.tabBarItem.tag = index
        
        let navVC = AWNavigationController(rootViewController: childController)
        
        addChildViewController(navVC)
        
    }
    
    private func addChildViewControllers() {
        
        addChildViewController(childController: AWHomeController(), title: "首页", imageName: "tabbar_home", index: 0)
        
        addChildViewController(childController: AWMessageController(), title: "消息", imageName: "tabbar_message_center", index: 1)
        
        addChildViewController(childController: AWDiscoverController(), title: "发现", imageName: "tabbar_discover", index: 2)
        
        addChildViewController(childController: AWMineController(), title: "我的", imageName: "tabbar_profile", index: 3)
        
    }
    
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        
        var index = 0
        
        for subView in tabBar.subviews {
            
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                if index == item.tag {
                    
                    for view in subView.subviews {
                        
                        if view.isKind(of: NSClassFromString("UIImageView")!) {
                            
                            view.transform = CGAffineTransform.init(scaleX: 0.5, y: 0.5)
                            
                            UIView.animate(withDuration: 0.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: {
                                
                                view.transform = CGAffineTransform.identity
                                
                            }, completion: nil)
                            
                        }
                        
                    }
                    
                }
                
                index += 1
                
            }
            
        }
        
    }
    
}

extension AWMainViewController: AWTabBarDelegate {
    
    func composeOperation() {
        
        print("我是代理回调")
        
    }
    
}
